$(function () {
    BASE.COMMON.getArea(2,'provinceId');
    BASE.USER_ADDERSS.getAddressList();
});
var BASE = BASE||{};
BASE.USER_ADDERSS = (function(){
    /*获取地址列表*/
    function getAddressList(){
        BASE.apiGet('/device/address/listForMe',function (res) {
           $('#addressList').html(template('addressList_tpl',{data:res}));
        })
    }
    /*添加地址*/
    function addAddress(obj,callback) {
        var sendData = _getAddaddressData();
        if(!sendData){
            return;
        }
        $(obj).attr('disabled',true);
        BASE.postHttp('/device/address/add',sendData,function (res) {
            if(res.code!=200){
                BASE.clearpop(res.message);
                $(obj).attr('disabled',false);
                return;
            }
            BASE.clearpop('保存成功');
            _initAdddata();
            getAddressList();
            if(callback&&typeof callback=='function'){
                callback(res.data.id,obj);
            }
        })
    }
   /* 删除地址*/
    function deleteAdderss(id) {
        var $this = $(this);
        BASE.apiPost('/device/address/delete',{id:id},function () {
            $this.parents('tr').slideUp().remove();
        });
    }
    /*修改地址前数据渲染*/
    function modifyAddressPrev(obj) {

        if(!$(obj).is(':checked')&&$(obj).hasClass('checkbox')){
            _initAdddata();
            return;
        }
        var provinceId = $(obj).data('provinceid');
        var cityId = $(obj).data('cityid');
        var areaId = $(obj).data('areaid');
        $('#provinceId').val(provinceId);
        BASE.COMMON.getArea(3,'cityId',provinceId,function () {
            $('#cityId').val(cityId);
            BASE.COMMON.getArea(4,'areaId',cityId,function () {
                $('#areaId').val(areaId);
            })
        });
        $('#modifyBtn').data('id', $(obj).data('id'));
        $('#address').val($(obj).data('address'));
        $('#postCode').val($(obj).data('postcode')||'');
        $('#tel').val($(obj).data('tel'));
        $('#name').val($(obj).data('name'));
        $('#defaultIs').prop('checked',$(obj).data('defaultis'));
        $('#saveBtn').hide();
        $('#modifyBtn').show();
    }
    /*修改地址*/
    function modifyAddress(obj,callback) {
        var sendData = _getAddaddressData();
        var id = $(obj).data('id');
        sendData.id = id;
        if(!sendData){
            BASE.OTHER_BUYBOOK.addReduceNum(0);
            return;
        }
        $(obj).attr('disabled',true);
        BASE.postHttp('/device/address/update',sendData,function (res) {
            if(res.ecode=='DATA_DB_EXCEPTION'||res.code==200){
                if(callback&&typeof callback=='function'){
                    callback(id,obj);
                    return;
                }
                _initAdddata();
                getAddressList();
                $(obj).attr('disabled',false);
            }else {
                BASE.clearpop(res.message);
                $(obj).attr('disabled',false);
            }
        })
    }
    /*设置默认地址*/
    function setDefautlAddress(id) {
        BASE.apiPost('/device/address/updateDefaultAddress',{id:id});
    }
    /*获取添加或修改地址的数据*/
    function _getAddaddressData() {
        var sendData = {
            address:$('#address').val(),
            postCode:$('#postCode').val(),	//邮编
            provinceId:$('#provinceId option:selected').val(),	//省份ID
            cityId:$('#cityId option:selected').val(),	//城市ID
            areaId:$('#areaId option:selected').val(),	//县/区ID
            tel:$('#tel').val(),	//联系电话
            name:$('#name').val(),	//收货人
            defaultIs:$('#defaultIs').is(':checked'), //是否默认
        };
        if(!sendData.provinceId){
            BASE.clearpop('请选择省');
            return false;
        }
        if(!sendData.cityId){
            BASE.clearpop('请选择市');
            return false;
        }
        if(!sendData.areaId){
            BASE.clearpop('请选择区');
            return false;
        }
        if(!sendData.address){
            BASE.clearpop('请填写详细地址');
            $('#address').focus();
            return false;
        }
        if(!sendData.name){
            BASE.clearpop('请填写联系人');
            $('#name').focus();
            return false;
        }
        if(!BASE.verifyTel(sendData.tel)){
            $('#tel').focus();
            return false;
        }
        return sendData;
    }
    
    /*初始化添加或修改的数据*/
    function _initAdddata() {
        $('#provinceId').val('');
        $('#cityId').val('');
        $('#areaId').val('');
        $('#modifyBtn').data('id', '');
        $('#address').val('');
        $('#postCode').val('');
        $('#tel').val('');
        $('#name').val('');
        $('#defaultIs').attr('checked',true);
        $('#saveBtn').show();
        $('#modifyBtn').hide();
    }
    return {
        getAddressList:getAddressList,
        addAddress:addAddress,
        deleteAdderss:deleteAdderss,
        setDefautlAddress:setDefautlAddress,
        modifyAddressPrev:modifyAddressPrev,
        modifyAddress:modifyAddress,
        initAdddata:_initAdddata,
    }
})();

